package com.nuanshui.heatedloan.order.integration.execute.handler.syshandler;

import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.order.integration.annotation.OrderExecuteAnnotation;
import com.nuanshui.heatedloan.order.integration.bean.OrderResult;
import com.nuanshui.heatedloan.order.integration.constant.OrderHandlerOrderNum;
import com.nuanshui.heatedloan.order.integration.execute.handler.OrderHandler;
import com.nuanshui.heatedloan.order.integration.form.OrderExecuteForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;

/**
 * 记录信息 handler
 * @Author: Gonzo
 * @Date 2017/4/13下午5:54
 * @since 1.4.2
 */
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@OrderExecuteAnnotation(value="logHandler", order= OrderHandlerOrderNum.ORDER_LOG)
public class LogHandler implements OrderHandler {

    private OrderHandler nextOrderhandler;
    private Logger logger = LoggerFactory.getLogger("appointment_order");


    @Override
    public OrderResult placeOrder(OrderExecuteForm form, OrderResult result) {
        LogUtil.info(logger, "----------开始执行 <生成订单>----------");
        try{
            nextOrderhandler.placeOrder(form, result);

            LogUtil.info(logger, String.format("----------结束执行 <生成订单>--%s----------", result.isSuccess() ? "" : "错误信息" + result.getMsg()));

        }catch(Exception e){
            LogUtil.error(logger, String.format("----------执行<生成订单> 出错! ---%s----------:", form), e);
        }
        return result;
    }

    @Override
    public OrderResult paySuccessOrder(OrderExecuteForm form, OrderResult result) {
        LogUtil.info(logger, "----------开始执行 <支付成功, 订单置为成功>----------");

        try{
            nextOrderhandler.paySuccessOrder(form, result);

            LogUtil.info(logger, String.format("----------结束执行 <支付成功, 订单置为成功>--%s----------", result.isSuccess() ? "" : "错误信息" + result.getMsg()));

        }catch(Exception e){
            LogUtil.error(logger, String.format("----------执行<支付成功, 订单置为成功> 出错! ----------", form), e);
        }
        return result;
    }

    @Override
    public OrderResult cancelOrder(OrderExecuteForm form, OrderResult result) {
        LogUtil.info(logger, String.format("----------开始执行 <取消订单>- 订单号%s----------", form.getOrderNum()));

        try{
            nextOrderhandler.cancelOrder(form, result);

            LogUtil.info(logger, String.format("----------结束执行 <取消订单>--%s----------", result.isSuccess() ? "" : "错误信息" + result.getMsg()));

        }catch(Exception e){
            LogUtil.error(logger, String.format("----------执行<取消订单> 出错! ----------", form), e);
        }
        return result;
    }

    @Override
    public OrderResult verifyAppointmentOrder(OrderExecuteForm form, OrderResult result) {
        LogUtil.info(logger, String.format("----------开始执行 <校验订单>- 二维码%s----------", form.getQrcodeMsg()));

        try{
            nextOrderhandler.verifyAppointmentOrder(form, result);

            LogUtil.info(logger, String.format("----------结束执行 <校验订单>--%s----------", result.isSuccess() ? "" : "错误信息" + result.getMsg()));

        }catch(Exception e){
            LogUtil.error(logger, String.format("----------执行<校验订单> 出错!----------", form), e);
        }
        return result;
    }

    @Override
    public void setNextOrderHandler(OrderHandler orderHandler) {
        this.nextOrderhandler = orderHandler;
    }
}
